/*
 * 
 */

package com.memorytech.starchive;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import jp.co.memory_tech.starchive.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.memorytech.starchive.db.SQLiteProvider;
import com.memorytech.starchive.entity.HistoryBarcode;
import com.memorytech.starchive.interfaceclass.OnHistoryBarcodeListener;

/**
 * The Class HistoryListProduct.
 * 
 * @author Tam-LT
 */
public class HistoryListProduct extends Fragment implements OnItemClickListener {

    /** The m callback. */
    OnHistoryBarcodeListener mCallback;
    private View view;
    private ListView listView;
    private ArrayList<HistoryBarcode> arrayList;

    /*
     * (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        SQLiteProvider provider = SQLiteProvider.getInstance(getActivity(), getActivity()
                .getPackageName());
        arrayList = provider.getListHistoryBarcode();
        for (HistoryBarcode historyBarcode : arrayList) {
            historyBarcode.setFreeSpace((float) (Math.random() * 100));
            historyBarcode.setUsedSpace((float) (historyBarcode.getFreeSpace() * 0.7));
        }
    }

    /*
     * (non-Javadoc)
     * @see
     * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
     * android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        arrayList = new ArrayList<HistoryBarcode>();

        view = inflater.inflate(R.layout.listhistory_pull, container, false);
        listView = (ListView) view.findViewById(R.id.pull_to_refresh_listview);
        listView.setAdapter(new HistoryListProductAdapter());
        listView.setOnItemClickListener(this);

        return view;
    }

    /*
     * (non-Javadoc)
     * @see android.support.v4.app.Fragment#onStart()
     */
    @Override
    public void onStart() {
        super.onStart();

        // When in two-pane layout, set the listview to highlight the selected
        // list item
        // (We do this during onStart because at the point the listview is
        // available.)
        if (getFragmentManager().findFragmentById(R.id.history_detail_fragment) != null) {
            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

    /*
     * (non-Javadoc)
     * @see android.support.v4.app.Fragment#onAttach(android.app.Activity)
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallback = (OnHistoryBarcodeListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    /**
     * The Class ListProductAdapter. It's used to display product item on list
     * view
     */
    public class HistoryListProductAdapter extends BaseAdapter {

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arrayList.size();
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getView(int, android.view.View,
         * android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View v;
            if (convertView == null) {

                LayoutInflater li = getActivity().getLayoutInflater();
                v = li.inflate(R.layout.history_product_item, null);
            } else {
                v = convertView;
            }
            final HistoryBarcode historyBarcode = arrayList.get(position);
            TextView barcode = (TextView) v
                    .findViewById(R.id.history_item_barcode);
            barcode.setText(historyBarcode.getBarcode());

            if (historyBarcode.getImage() != null) {
                ImageView imageView = (ImageView) v.findViewById(R.id.history_item_image);

                byte[] bytes = historyBarcode.getImage();
                ByteArrayInputStream imageStream = new ByteArrayInputStream(bytes);
                Bitmap theImage = BitmapFactory.decodeStream(imageStream);
                imageView.setImageBitmap(theImage);
            }
            v.findViewById(R.id.history_item_detail).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mCallback.onDetailItemSelected(historyBarcode);
                }
            });

            return v;
        }

    }

    /*
     * (non-Javadoc)
     * @see
     * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
     * .AdapterView, android.view.View, int, long)
     */
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        // Notify the parent activity of selected item
        HistoryBarcode object = arrayList.get(arg2);
        mCallback.onHistoryBarcodeSelected(object);
        // Set the item as checked to be highlighted when in two-pane layout
        listView.setItemChecked(arg2, true);

    }

}
